package com.ruoyi.algorithm.controller;

import com.ruoyi.algorithm.entity.TableStruct;
import com.ruoyi.algorithm.model.AlgorithmHistoryModel;
import com.ruoyi.algorithm.model.AlgorithmListModel;
import com.ruoyi.algorithm.model.AlgorithmModel;
import com.ruoyi.algorithm.model.AlgorithmOnLineStateModel;
import com.ruoyi.algorithm.service.AlgorithmService;
import com.ruoyi.algorithm.service.TableStructService;
import com.beihu.sightplus.common.authorization.entity.User;
import com.beihu.sightplus.common.authorization.service.UserAuthorization;
import com.beihu.sightplus.common.request.PageRequest;
import com.beihu.sightplus.common.response.PageAndConditionResult;
import com.beihu.sightplus.common.response.PageResult;
import com.beihu.sightplus.common.response.ResultData;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/algorithm")
@Api(value = "算法API", tags = {"算法API"})
public class AlgorithmController {
    @Autowired
    private AlgorithmService algorithmService;

    @Autowired
    private TableStructService tableStructService;

    /**
     * 新增算法
     */
    @ApiOperation("新增算法")
    @PostMapping("/add")
    public ResultData add(@RequestBody AlgorithmModel algorithmModel){
        algorithmService.add(algorithmModel);
        return ResultData.success();
    }

    /**
     * 修改，当算子发生变化时，需要往算法历史表中添加一条新的数据，否则，算法表和算法历史表的数据，均进行修改操作
     */
    @ApiOperation("修改算法")
    @PostMapping("/update")
    public ResultData update(@RequestBody AlgorithmModel algorithmModel) {
        algorithmService.update(algorithmModel);
        return ResultData.success();
    }

    /**
     * 删除
     */
    @ApiOperation("删除算法")
    @ApiImplicitParam(name = "algorithmId", value = "算法Id", required = true)
    @DeleteMapping("/delete/{algorithmId}")
    public ResultData delete(@PathVariable Long algorithmId) {
        algorithmService.delete(algorithmId);
        return ResultData.success();
    }

    /**
     * 算法上下线
     */
    @ApiOperation("算法上下线")
    @PostMapping("/setOnlineState")
    public ResultData setOnlineState(@RequestBody AlgorithmOnLineStateModel algorithmOnLineStateModel) {
        algorithmService.setOnlineState(algorithmOnLineStateModel);
        return ResultData.success();
    }

    /**
     * 查询算法详情信息
     */
    @ApiOperation("查询算法详情信息")
    @ApiImplicitParam(name = "algorithmId", value = "算法Id", required = true)
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "算法详情信息", response = AlgorithmModel.class)
    })
    @GetMapping("/detail/{algorithmId}")
    public ResultData detail(@PathVariable("algorithmId") Long algorithmId) {
        return ResultData.success(algorithmService.detail(algorithmId));
    }

    /**
     * 根据算法Id和算子Id,查询历史算法信息
     */
    @ApiOperation("根据算法Id和算子Id,查询历史算法信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "algorithmId", value = "算法Id", required = true),
            @ApiImplicitParam(name = "operatorId", value = "算子Id", required = true)
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", response = AlgorithmHistoryModel.class)
    })
    @GetMapping("getHistoryByAlgorithmIdAndOperatorId")
    public ResultData getAlgorithmHistoryByAlgorithmIdAndOperatorId(Long algorithmId, Long operatorId) {
        return ResultData.success(algorithmService.getAlgorithmHistoryByAlgorithmIdAndOperatorId(algorithmId, operatorId));
    }

    /**
     * 分页查询
     */
    @ApiOperation("分页查询")
    @GetMapping("/pageList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderAttr", value = "需要排序的属性"),
            @ApiImplicitParam(name = "orderType", value = "排序的方式，Asc：升序，Desc：降序"),
            @ApiImplicitParam(name = "current", value = "当前页码"),
            @ApiImplicitParam(name = "size", value = "每页展示数据的条数")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", response = AlgorithmListModel.class)
    })
    public PageAndConditionResult pageList(PageRequest pageRequest, AlgorithmListModel algorithmListModel) {
        // 需要展示的数据
        String orderAttr = pageRequest.getOrderAttr();
        String orderType = pageRequest.getOrderType();
        Long current = pageRequest.getCurrent();
        Long size = pageRequest.getSize();

        PageResult pageResult = algorithmService.listByPage(orderAttr, orderType, current, size, algorithmListModel);

        // 字段展示的顺序
        User userAuthorization = UserAuthorization.getUserAuthorization();
        Long userId = userAuthorization.getId();
        List<TableStruct> tableField = tableStructService.getTableField(userId, "algorithm");

        PageAndConditionResult pageAndConditionResult = new PageAndConditionResult();
        pageAndConditionResult.setFields(tableField);
        pageAndConditionResult.setPage(pageResult);
        return pageAndConditionResult;
    }

    @ApiOperation("根据算子跳转算法列表页面")
    @ApiImplicitParam(name = "operatorId", value = "算力Id", required = true)
    @GetMapping("/getPageListByOperatorId/{operatorId}")
    public PageAndConditionResult getPageListByOperatorId(@PathVariable("operatorId") Long operatorId){
        // 需要展示的数据
        AlgorithmListModel algorithmListModel = new AlgorithmListModel();
        algorithmListModel.setOperatorId(operatorId);
        PageRequest pageRequest = new PageRequest();
        String orderAttr = pageRequest.getOrderAttr();
        String orderType = pageRequest.getOrderType();
        Long current = pageRequest.getCurrent();
        Long size = pageRequest.getSize();
        PageResult pageResult = algorithmService.listByPage(orderAttr, orderType, current, size, algorithmListModel);

        // 字段展示的顺序
        User userAuthorization = UserAuthorization.getUserAuthorization();
        Long userId = userAuthorization.getId();
        List<TableStruct> tableField = tableStructService.getTableField(userId, "algorithm");

        PageAndConditionResult pageAndConditionResult = new PageAndConditionResult();
        pageAndConditionResult.setFields(tableField);
        pageAndConditionResult.setPage(pageResult);
        return pageAndConditionResult;
    }
}
